package me.jollyfly.jshop.item.server.controller

import com.github.pagehelper.PageHelper
import me.jollyfly.jshop.common.bean.PageResultBean
import me.jollyfly.jshop.common.bean.ResultBean
import me.jollyfly.jshop.item.client.api.ItemApi
import me.jollyfly.jshop.item.client.model.ItemCategoryDO
import me.jollyfly.jshop.item.client.model.ItemDO
import me.jollyfly.jshop.item.client.model.bo.RecommendationBO
import me.jollyfly.jshop.item.server.dao.ItemCatDAO
import me.jollyfly.jshop.item.server.dao.ItemDAO
import me.jollyfly.jshop.item.server.service.ItemCatService
import me.jollyfly.jshop.item.server.service.ItemService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import java.util.*


/**

 */
@RestController
open class ItemController @Autowired
constructor(private val itemDAO: ItemDAO, private val itemCatDAO: ItemCatDAO,
            private val itemCatService: ItemCatService) : ItemApi {

    @Autowired
    private var itemService: ItemService? = null

    /**
     * 根据商品ID返回商品信息

     * @param itemId 商品ID
     * *
     * *
     * @return ResultBean封装返回实体类 @see ResultBean
     * *
     * *
     * @author jollyfly
     */
    override fun getItemById(@PathVariable("id") itemId: Long?): ResultBean<ItemDO> {
        val item = this.itemDAO.selectByPrimaryKey(itemId)
        return ResultBean(item)
    }

    /**
     * 根据父类目Id获取非文件夹子类目

     * @param parnetId 父类目ID
     * *
     * *
     * @return ResultBean
     * *
     * *
     * @author jollyfly
     */
    override fun listCategoryByParent(@PathVariable("id") parnetId: Long?): ResultBean<List<ItemCategoryDO>> {
        val itemCatList = this.itemCatDAO.listCategoryByParent(parnetId)
        itemCatList.sortWith(Comparator.comparingInt<ItemCategoryDO>({ it.sortOrder!! }))
        return ResultBean(itemCatList)
    }

    /**
     * 根据类目Id分页获取该类目下所有商品

     * @param cid 父类目ID
     * *
     * *
     * @return ResultBean
     * *
     * *
     * @author jollyfly
     */
    override fun listItemByCategory(cid: Long?, currentNum: Int, pageSize: Int): PageResultBean<List<ItemDO>> {

        val item = ItemDO()
        item.cid = cid
        item.status = java.lang.Byte.valueOf("1")
        val resultBean = PageResultBean<List<ItemDO>>()
        //分页查询
        val itemPage = PageHelper.startPage<Any>(currentNum, pageSize).doSelectPageInfo<ItemDO> {
            this.itemDAO.select(item)
        }
        //封装返回值
        resultBean.totalCount = itemPage.total
        resultBean.data = itemPage.list
        return resultBean

    }

    /**
     * 根据类目Id获取该类目下所有推荐商品

     * @param cid 父类目ID
     * *
     * *
     * @return ResultBean
     * *
     * *
     * @author jollyfly
     */
    override fun listRecommendationByCat(@PathVariable("categoryId") cid: Long?): ResultBean<List<ItemDO>> {
        val recommendationList = this.itemDAO.listRecommendation(cid)
        return ResultBean(recommendationList)
    }


    /**
     * 获取所有推荐商品

     * @return ResultBean
     * *
     * *
     * @author jollyfly
     */
    override fun listRecommendation(): ResultBean<List<RecommendationBO>> {
        val recommendationList = this.itemCatDAO.listAllRecommendation()
        return ResultBean(recommendationList)
    }


    override fun listCategory(): ResultBean<List<ItemCategoryDO>> {
        return ResultBean(itemCatService.listCategory())
    }


    /**
     * 加载商品类别选择器数据
     *
     */
    override fun listCatSelector(): ResultBean<List<ItemCategoryDO>> {
        return ResultBean(itemCatService.listCatSelector())
    }

    override fun addItem(@RequestBody(required = true) itemDO: ItemDO): ResultBean<Boolean> {
        return ResultBean(itemService!!.addItem(itemDO))
    }

}
